<template>
  <div>
    <mt-search @keyup.enter.native="searchLocel" v-model="search" placeholder="搜索" :show="showResult">
      <mt-cell v-for="item in result" :title="item.title" :to="item.link" :is-link="item.islink">
        {{item.value}}&nbsp;&nbsp;

        <mt-button v-show="item.showbotton" @click.native="addTask(item.url)" size="small" type="primary">添加</mt-button>
      </mt-cell>
      <mt-button @click.native="searchRemote" style="margin-top:25px;" v-show="showSearchRemote" size="large">
        load站外小说 (～ o ～)~zZ...

      </mt-button>
    </mt-search>
  </div>
</template>

<style lang="css">

</style>

<script type="text/babel">
import {MessageBox} from 'mint-ui'
import {Indicator} from 'mint-ui'

export default{
  data () {
    return {
      search: '',
      result: [],
      showResult: true,
      showSearchRemote: false
    }
  },
  methods: {
    searchLocel: function () {
      Indicator.open({
        text: '加载中...',
        spinnerType: 'fading-circle'
      })
      var _this = this
      _this.result = []
      _this.$http.post('https://novel.nightc.com/v1/search/local', {
        title: _this.search
      }).then((resp) => {
        var body = resp.body
        if (body !== undefined && body.code === 0) {
          for (var i = 0; i < body.data.length; i++) {
            var searchData = {}
            searchData.title = body.data[i].title
            searchData.value = body.data[i].auth
            searchData.link = '/novel/' + body.data[i].id
            searchData.islink = true
            searchData.url = ''
            searchData.showbotton = false
            _this.result.push(searchData)
          }
          Indicator.close()
          _this.showSearchRemote = true
          _this.showResult = true
        }
      }, (resp) => {
        Indicator.close()
        var body = resp.body
        if (body !== undefined) {
          MessageBox('提示', body.data)
        }
      })
    },
    searchRemote: function () {
      Indicator.open({
        text: '加载中...',
        spinnerType: 'fading-circle'
      })
      var _this = this
      _this.showSearchRemote = false
      _this.$http.post('https://novel.nightc.com/v1/search/remote', {
        title: _this.search
      }).then((resp) => {
        var body = resp.body
        if (body !== undefined && body.code === 0) {
          var data = body.Data
          if (data.length === 0) {
            MessageBox('提示', '抱歉找不到此小说')
          }
          for (var i = 0; i < data.length; i++) {
            var searchData = {}
            if (data[i] !== undefined && data[i].Novel !== undefined) {
              searchData.title = data[i].Novel.Title
              searchData.value = data[i].Novel.Auth
              searchData.link = 'search'
              searchData.url = data[i].Novel.From
              searchData.islink = false
              searchData.showbotton = true
              _this.result.push(searchData)
            }
          }
          Indicator.close()
          _this.showResult = true
        }
      }, (resp) => {
        Indicator.close()
        var body = resp.body
        if (body !== undefined) {
          MessageBox('提示', body.data)
        }
      })
    },
    addTask: function (url) {
      var _this = this
      Indicator.open({
        text: '加载中...',
        spinnerType: 'fading-circle'
      })
      _this.$http.post('https://novel.nightc.com/v1/novel/task', {
        url: url
      }).then((resp) => {
        var body = resp.body
        Indicator.close()
        if (body !== undefined && body.code === 0) {
          MessageBox('^_^', '后台添加中^_^')
        }
      }, (resp) => {
        Indicator.close()
        var body = resp.body
        if (body !== undefined) {
          MessageBox('提示', body.data)
        }
      })
    }
  }
}
</script>
